package flc.ast.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.UdpCombAdapter;
import flc.ast.bean.MyUdpCombBean;
import flc.ast.databinding.ActivityUdpCombBinding;
import flc.ast.dialog.DeleteDialog;
import flc.ast.utils.AppSpUtils;

public class UdpCombActivity extends BaseAc<ActivityUdpCombBinding> {

    private UdpCombAdapter udpCombAdapter;
    private List<MyUdpCombBean> listShow = new ArrayList<>();
    private int selPosition;
    private boolean isEdit;

    @Override
    protected int onCreate() {
        return R.layout.activity_udp_comb;
    }

    @Override
    protected void initView() {
        mDataBinding.ivUdpCombBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvAdd.setOnClickListener(this);
        mDataBinding.tvAddConn.setOnClickListener(this);
        mDataBinding.tvUdpCombEdit.setOnClickListener(this);
        mDataBinding.tvUdpCombComplete.setOnClickListener(this);

        mDataBinding.rvUdpCombList.setLayoutManager(new LinearLayoutManager(mContext));
        udpCombAdapter = new UdpCombAdapter();
        mDataBinding.rvUdpCombList.setAdapter(udpCombAdapter);
        udpCombAdapter.addChildClickViewIds(R.id.ivUdpCombItemDelete);
        udpCombAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvUdpCombEdit) {//编辑
            if (listShow == null || listShow.size() == 0) return;
            isEdit = true;
            udpCombAdapter.setEdit(isEdit);
            udpCombAdapter.notifyDataSetChanged();
            mDataBinding.tvUdpCombEdit.setVisibility(View.GONE);
            mDataBinding.tvUdpCombComplete.setVisibility(View.VISIBLE);
        } else if (id == R.id.tvUdpCombComplete) {//完成
            cancelEdit();
        } else if (id == R.id.tvAdd || id == R.id.tvAddConn) {//添加
            startActivity(AddUdpCombActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivUdpCombItemDelete://删除
                selPosition = position;
                showDeleteDialog();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    //删除
    private void delete() {
        listShow.remove(selPosition);
        AppSpUtils.saveUdpCombList(listShow);
        getData();
        ToastUtils.showShort(getString(R.string.delete_suc));
    }

    private void getData() {
        listShow = AppSpUtils.getUdpCombList();
        if (listShow != null && listShow.size() != 0) {
            udpCombAdapter.setList(listShow);
            mDataBinding.rvUdpCombList.setAdapter(udpCombAdapter);
            mDataBinding.rvUdpCombList.setVisibility(View.VISIBLE);
            mDataBinding.llUdpCombNoData.setVisibility(View.GONE);
            mDataBinding.tvAddConn.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvUdpCombList.setVisibility(View.GONE);
            mDataBinding.llUdpCombNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvAddConn.setVisibility(View.GONE);
        }
    }

    private void cancelEdit() {
        isEdit = false;
        udpCombAdapter.setEdit(isEdit);
        udpCombAdapter.notifyDataSetChanged();
        mDataBinding.tvUdpCombEdit.setVisibility(View.VISIBLE);
        mDataBinding.tvUdpCombComplete.setVisibility(View.GONE);
    }

    private void showDeleteDialog() {
        DeleteDialog dialog = new DeleteDialog(mContext);
        dialog.setListener(new DeleteDialog.Ilistener() {

            @Override
            public void clickDelete() {
                delete();
            }

        });
        dialog.show();
    }

}